<template>
  <div class="app-container">
    <el-card>
      <el-button type="primary" @click="goIndex" style="margin-bottom: 20px"
        >返回主页</el-button
      >
      <el-form
        :model="queryParams"
        ref="queryForm"
        :inline="true"
        v-show="showSearch"
      >
        <el-form-item :label="forWho + '姓名'" prop="childName">
          <el-input
            v-model="queryParams.childName"
            :placeholder="'请输入' + forWho + '姓名'"
            clearable
            size="small"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item :label="booker + '手机号码'" prop="phone">
          <el-input
            v-model="queryParams.phone"
            :placeholder="'请输入' + booker + '手机号码'"
            clearable
            size="small"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            icon="el-icon-search"
            size="mini"
            @click="handleQuery"
            >搜索</el-button
          >
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
            >重置</el-button
          >
        </el-form-item>
      </el-form>
      <el-table
        v-if="tableData && tableData.length > 0"
        :data="tableData"
        stripe
        border
        style="width: 100%"
      >
        <el-table-column :label="forWho + '姓名'">
          <template slot-scope="scope">
            {{
              "***" +
              scope.row.childName.substring(scope.row.childName.length - 1)
            }}
          </template>
        </el-table-column>
        <el-table-column label="预约日期" prop="bookDate" />
        <el-table-column label="预约时间段">
          <template slot-scope="scope">
            {{ scope.row.startTime }}至{{ scope.row.endTime }}
          </template>
        </el-table-column>
        <el-table-column :label="personReserved + '姓名'" align="center">
          <template slot-scope="scope">
            <span>{{ scope.row.userNickname }}</span>
          </template>
        </el-table-column>
        <el-table-column label="预约状态">
          <template slot-scope="scope">
            <el-tag type="success" v-if="scope.row.status == 1">完成</el-tag>
            <el-tag type="info" v-if="scope.row.status == 0">已预约</el-tag>
            <el-tag type="danger" v-if="scope.row.status == 2">爽约</el-tag>
            <el-tag type="warning" v-if="scope.row.status == 3">已取消</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="380">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="warning"
              v-if="
                notOverDue(scope.row.bookDate, scope.row.endTime) &&
                scope.row.status == 0 &&
                (scope.row.childName == queryParams.childName ||
                  scope.row.phone == queryParams.phone)
              "
              @click="signStatus(3, '取消预约', scope.row)"
              >取消预约</el-button
            >
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </el-card>
  </div>
</template>

<script>
import { listBook, updateBook, selectBookNoCancel } from "@/api/parent/book";
export default {
  name: "cancelBook",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 预约表格数据
      bookList: [],
      tableData: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        childName: null,
        monthAge: null,
        email: null,
        phone: null,
        userId: null,
        userNickname: null,
        bookDate: null,
        startTime: null,
        endTime: null,
        status: null,
      },
      // 表单参数
      form: {},
      personReserved: "医生",
      forWho: "孩子",
      booker: "家长",
      updatebody: {},
    };
  },
  created() {
    this.getConfigKey("sys.booker").then((response) => {
      this.booker = response.msg;
    });
    this.getConfigKey("sys.personReserved").then((response) => {
      this.personReserved = response.msg;
    });
    this.getConfigKey("sys.forWho").then((response) => {
      this.forWho = response.msg;
    });
  },
  computed: {
    notOverDue() {
      return (date,time)=> {
        return this.judgeNotOverdue(date,time)
      };
    },
  },
  methods: {
    /** 查询预约列表 */
    getList() {
      if (this.queryParams.childName || this.queryParams.phone) {
        this.loading = true;
        selectBookNoCancel(this.queryParams).then((response) => {
          if (response.rows.length < 1) {
            this.$alert("该用户不存在非取消状态的预约");
          }
          this.tableData = [];
          this.tableData = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      } else {
        this.$modal.msg("姓名和手机号至少填一个才能查询到预约哦!");
      }
    },
    getListNoAlert() {
      this.loading = true;
      selectBookNoCancel(this.queryParams).then((response) => {
        this.tableData = [];
        this.tableData = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    signStatus(status, msg, row) {
      if(status==3&&!this.judgeNotOverdue(row.bookDate,row.endTime)) {
        this.$message.warning("该预约已过期")
        return
      }
      var originStatus = row.status;
      this.updatebody = row;
      var v = this;
      var info = "";
      if (!v.queryParams.phone || !v.queryParams.childName) {
        if (!v.queryParams.phone) {
          info = "手机号";
        } else if (!v.queryParams.childName) {
          info = "姓名";
        }
        var res = "";
        this.$prompt("请输入" + info, "信息验证", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          closeOnClickModal: false,
        })
          .then(({ value }) => {
            if (
              (!this.queryParams.phone && value == this.updatebody.phone) ||
              (!this.queryParams.childName &&
                value == this.updatebody.childName)
            ) {
              v.updatebody.status = status;
              updateBook(v.updatebody).then((response) => {
                v.getListNoAlert();
                v.$modal.msgSuccess(msg + "成功");
                res = response;
              });
            } else {
              row.status = originStatus;
              v.$message.error("验证失败，" + info + "输入错误");
            }
          })
          .catch((e) => {
            row.status = originStatus;
            v.$modal.msg(msg + "失败", e);
          });
      } else {
        this.$modal
          .confirm("确认要" + msg + "吗？")
          .then(function () {
            v.updatebody.status = status;
            return updateBook(v.updatebody);
          })
          .then(() => {
            v.getListNoAlert();
            v.$modal.msgSuccess(msg + "成功");
          })
          .catch(function () {
            row.status = originStatus;
            v.$modal.msg(msg + "失败");
          });
      }
    },
    goIndex() {
      this.$router.push("/");
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.tableData = [];
      this.total = 0;
      this.queryParams.pageNum = 1;
      this.queryParams.pageSize = 10;
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
  },
};
</script>
